package com.otherTest.java8.tomap;


import lombok.Data;
import org.junit.Test;

import java.time.LocalDate;
import java.time.Month;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description:
 * @author: conceit
 * @Date: 2018/7/20 0020
 * @see com.otherTest.java8.tomap.TestToMap
 */
@SuppressWarnings("all")
public class TestToMapTwo {

    /**
     * @Description: 第二个方法
     * 对男性以及女性进行分组，假如流里只有两种性别，那么分组也就只有两组key.每一个key的value以我们后来指定的CharSequence为分隔符切割作为整体的字符串value
     * toMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper, BinaryOperator<U> mergeFunction)
     * @param:
     * @return:
     * @author: conceit
     * @Date: 2018/7/20
     */
    @Test
    public void doTestExampleOne() {
        //{MALE=Jack,Jode,Jason, FEMALE=Jake,Jane,Jeny}
        Map<Employee.Gender, String> genderToNamesMap = TestToMapTwo.persons()
                .stream()
                .collect(Collectors.toMap(
                        Employee::getGender, Employee::getName,
                        (oldValue, newValue) -> String.join(",", oldValue, newValue)));
        System.out.println(genderToNamesMap);
    }

    /**
     * @Description: 对男性以及女性进行分组, 分别统计出每个key下的count
     * @author: conceit
     * @Date: 2018/7/20
     */
    @Test
    public void doTestExampleTwo() {
        Map<Employee.Gender, Long> genderKeyCount = TestToMapTwo.persons().stream().collect(Collectors.toMap(
                Employee::getGender, count -> 1L, (oldCount, newCount) -> oldCount + newCount
        ));
        System.out.println(genderKeyCount);
    }


    public static List<Employee> persons() {
        Employee p1 = new Employee(1, "Jake", Employee.Gender.FEMALE, LocalDate.of(1971,
                Month.JANUARY, 1), 2343.0);
        Employee p2 = new Employee(2, "Jack", Employee.Gender.MALE, LocalDate.of(1972,
                Month.JULY, 21), 7100.0);
        Employee p3 = new Employee(3, "Jane", Employee.Gender.FEMALE, LocalDate.of(1973,
                Month.MAY, 29), 5455.0);
        Employee p4 = new Employee(4, "Jode", Employee.Gender.MALE, LocalDate.of(1974,
                Month.OCTOBER, 16), 1800.0);
        Employee p5 = new Employee(5, "Jeny", Employee.Gender.FEMALE, LocalDate.of(1975,
                Month.DECEMBER, 13), 1234.0);
        Employee p6 = new Employee(6, "Jason", Employee.Gender.MALE, LocalDate.of(1976,
                Month.JUNE, 9), 3211.0);

        List<Employee> persons = Arrays.asList(p1, p2, p3, p4, p5, p6);

        return persons;
    }

    @Data
    public static class Employee {

        public enum Gender {
            MALE, FEMALE, Other
        }

        private long id;
        private String name;
        private Gender gender;
        private LocalDate dob;
        private double income;

        public Employee(long id, String name, Employee.Gender gender, LocalDate dob,
                        double income) {
            this.id = id;
            this.name = name;
            this.gender = gender;
            this.dob = dob;
            this.income = income;
        }

        public Gender getGender() {
            return gender;
        }

        public long getId() {
            return id;
        }

        public String getName() {
            return name;
        }

    }

}
